<?php
header ("Content-Type:text/html; charset= utf-8");
require_once ('config.php');
require_once (WEBURL.'/include/conn.php');
require_once ('login_status_check.php');
if(!isset($_POST['title'])){
	echo "forbidden";
	exit();
}
if(!checkLogin()){
	$gotopage=base64_encode("article_add.php");
	header("Location:admin_login.php?gotopage=".$gotopage);
}
else{
	require_once (WEBURL.'/channel.class.php');
	require_once (WEBURL.'/time_to_timestamp.php');
	//require_once (XunSearchPath.'/sdk/php/lib/XS.php');
	$article_title=addslashes($_POST['title']);
	$article_is_extends_url=0;
	$article_extends_url="";
	if(!empty($_POST['links'])){
		$article_is_extends_url=1;
		$article_extends_url=$_POST['external_links'];
	}
	$article_from=$_POST['from'];
	$article_writer=$_POST['writer'];
	$article_main_channel=$_POST['main_channel'];
	$article_status=1;
	$article_has_picture=0;
	$article_picture_path="";
	if($_POST['picture']!=""){
		$article_has_picture=1;
		$article_picture_path=$_POST['picture'];
	}
	$article_clicked_times=$_POST['clicked_times'];
	$article_publish_time=timeToStimestamp($_POST['publish_time']);
	$article_content=base64_encode($_POST['article_content']);
	$article_is_top=$_POST['istop'];
	$article_is_top_time=0;
	if($article_is_top=="1"){
		$article_is_top_time_tmp=$_POST['is_top_time'];
		switch($article_is_top_time_tmp){
			case 0:
				$article_is_top_time=$article_publish_time+60*60*24*7;
				break;
			case 1:
				$article_is_top_time=$article_publish_time+60*60*24*30;
				break;
			case 2:
				$article_is_top_time=$article_publish_time+60*60*24*30*3;
				break;
			case 3:
				$article_is_top_time=$article_publish_time+60*60*24*30*5;
				break;
			case 4:
				$article_is_top_time=$article_publish_time+60*60*24*365;
				break;
			case 5:
				$article_is_top_time= timeToStimestamp($_POST['custom_top_time']);
				break;
			default:
				break;

		}
	}
	$current_time=time();
	$sql_save_article="insert into article_normal(article_title,article_is_extends_url,article_extends_url,article_from,article_writer,article_main_channel,article_special_topic,
	article_has_picture,article_picture_path,article_click,article_publish_time,article_modify_time,article_status,article_is_top,article_top_time,article_content)values('$article_title',$article_is_extends_url,'$article_extends_url','$article_from','$article_writer',
	$article_main_channel,'',$article_has_picture,'$article_picture_path',$article_clicked_times,$article_publish_time,$current_time,$article_status,$article_is_top,$article_is_top_time,
    '$article_content');";
	//echo $sql_save_article;
	$result_save_article=mysql_query($sql_save_article) or die(mysql_error());
	if($result_save_article){
		$sql_tmp="select MAX(article_id) from article_normal;";
		$result=mysql_query($sql_tmp);
		$row=mysql_fetch_row($result);
		$article_id=$row[0];
			/*更新搜索表*/
			$article_content=@strip_tags(base64_decode($article_content));
			$article_content=str_replace('&nbsp', '', $article_content);
			$article_content=str_replace('&ldquo', '', $article_content);
			$article_content=str_replace('&rdquo', '', $article_content);
			$article_content=str_replace(';', '', $article_content);
			$article_content=str_replace('；', '', $article_content);
			$article_content=@addslashes($article_content);
			$sql_in="insert into search(id,article_title,article_main_channel,article_publish_time,article_modify_time,article_from,article_content)
        values($article_id,'$article_title',$article_main_channel,'$article_publish_time','$current_time','$article_from','$article_content')";
			$result_in=mysql_query($sql_in) or die(mysql_error());
			/*添加索引
			
			$xs = new XS('sys');
			$index = $xs->index;
			$data = array(
         'id' => $article_id,
         'article_title'=> $article_title,
         'article_main_channel'=> $article_main_channel,
         'article_publish_time'=> $article_publish_time,
         'article_modify_time'=> $current_time,
         'article_from'=> $article_from,
         'article_content'=> $article_content 
			);
			创建文档对象
			$doc = new XSDocument;
			$doc->setFields($data);
			添加到索引数据库中
			$index->add($doc); */
		echo "<script language=JavaScript> alert('发表文章成功！'); window.location.href='article_add.php';</script>";
	}

}